package com.star.niuke;

/**
 * @author: liminghui
 * @date: 2021/7/25 18:32
 * @version: 1.0
 * @description: 二分法查找
 */
public class Search2 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 4, 4, 5};
        Search2 search2 = new Search2();
        int target = 4;
        int search = search2.search(nums, target);
        System.out.println(search);
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * <p>
     * 如果目标值存在返回下标，否则返回 -1
     *
     * @param nums   int整型一维数组
     * @param target int整型
     * @return int整型
     */
    public int search(int[] nums, int target) {
        int low = 0;
        int high = nums.length - 1;
        int mid = 0;
        while (low <= high) {
            mid = low + (high - low) / 2;
            if (nums[mid] == target) {
                while (mid != 0 && (nums[mid - 1] == nums[mid])) {
                    mid--;
                }
                return mid;
            } else if (nums[mid] > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }

}
